// 2020.07.01

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void letter_stat(int letter_cnt[], char * word)
{
    int word_length = strlen(word);
    for(int i = 0; i < word_length; i++)
    {
        if(word[i]>='a'&&word[i]<='z')
        {
            letter_cnt[word[i]-'a'] ++;
        }
        else if(word[i]>='A'&&word[i]<='Z')
        {
            letter_cnt[word[i]-'A'] ++;
        }
    }
}

int main()
{
    FILE* inputFile = fopen("passage.txt","r");
    char word[200];
    char longest_word[50][200];
    int longest_word_cnt = 0;
    int longest_word_length = 0;
    int letter_cnt[26];

    int numbers[1000];
    int numbers_cnt = 0;
    memset(letter_cnt, 0, sizeof(int)*26);

    int word_cnt = 0;

    while(fscanf(inputFile, "%s", word) == 1)
    {
        bool if_has_int = false;
        int word_length = strlen(word);
        for(int i = 0; i < word_length; i++)
        {
            if (word[i]>='0'&&word[i]<='9') if_has_int = true;
        }

        letter_stat(letter_cnt, word);
        
        word_cnt++;

        if (word_length > longest_word_length){
            longest_word_cnt = 1;
            strcpy(longest_word[longest_word_cnt - 1], word);
            longest_word_length = word_length;
        }
        else if (longest_word_cnt < 50 && word_length == longest_word_length)
        {
            longest_word_cnt++;
            strcpy(longest_word[longest_word_cnt - 1], word);
        }

        if (if_has_int) {
            int a = atoi(word);
            numbers[numbers_cnt++] = a;
        }
    }

    for (int i = 0; i < 26; i++)
    {
        printf("%c(%c): %d\n", 'A'+i, 'a'+i, letter_cnt[i]);
    }

    printf("Total Words: %d\n", word_cnt);
    printf("Longest words (Length: %d):\n",longest_word_length);

    int i = 0;
    while(i < longest_word_cnt) {
        printf("%s\n", longest_word[i]);
        i++;
    }

    for(i = 0; i < numbers_cnt; i++)
    {
        printf("%d ", numbers[i]);
    }

    fclose(inputFile);

    return 0;
}
